package xyc.szk.web.servlet.config;
/*
 * @Description: ServletConfig
 * @Author: 山志凯
 * @Date: 2021-03-17
 * @Time: 08:19
 */

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

@WebServlet(value = "/config01",initParams = {@WebInitParam(name="username",value="张三"),
        @WebInitParam(name="sex",value="男")})
public class ConfigServlet01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //只能读取本servlet的配置信息
        ServletConfig config = this.getServletConfig(); //它是被tomcat服务器实例化的
        //System.out.println(config); //org.apache.catalina.core.StandardWrapperFacade实例
        PrintWriter pw = resp.getWriter();

        // 读<servlet-name>
        // System.out.println(config.getServletName()); //demoServletConfig
        // pw.print(config.getServletName());

        //读<init-param>key-value servlet 初始化参数
        // String encoding = config.getInitParameter("encoding");
        // pw.print(encoding);

        //读取所有的初始化参数
        Enumeration<String> initNames = config.getInitParameterNames();
        while (initNames.hasMoreElements()) {
            String key = initNames.nextElement(); //获取配置参数的name
            String value = config.getInitParameter(key); //获取配置参数中name对应的value
            pw.print(key+"----"+value);
            pw.print("\n");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doPost(req, resp);
        doPost(req,resp);
    }

}
